package com.hwh.traffic.utils;

import java.math.BigDecimal;

public class DoubleUtil {
    /**
     * double 相加
     * @param d1
     * @param d2
     * @return
     */
    public static double sum(double d1,double d2){
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.add(bd2).doubleValue();
    }


    /**
     * double 相减
     * @param d1
     * @param d2
     * @return
     */
    public static double sub(double d1,double d2){
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.subtract(bd2).doubleValue();
    }

    /**
     * double 乘法
     * @param d1
     * @param d2
     * @return
     */
    public static double mul(double d1,double d2){
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.multiply(bd2).doubleValue();
    }


    /**
     * double 除法
     * @param d1
     * @param d2
     * @param scale 四舍五入 小数点位数
     * @return
     */
    public static double div(double d1,double d2,int scale){
        //  当然在此之前，你要判断分母是否为0，
        //  为0你可以根据实际需求做相应的处理
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.divide(bd2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * double 转 string 去掉后面锝0
     * @param i
     * @return
     */
    public static String getString(double i){
        String s = String.valueOf(i);
        if(s.indexOf(".") > 0){
            //正则表达
            s = s.replaceAll("0+?$", "");//去掉后面无用的零
            s = s.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return s;
    }

    public static void main(String[] args) {
        String i=numberToBits("10000000.01");
        System.out.println(i);
    }
    /**
     * 数字转换为千位符
     * @param number
     * @return
     */
    public static String numberToBits(String number){
        String begin="";
        String end="";
        String[] num=number.split("\\.");
        if(num.length>1){
            begin=num[0];
            end=num[1];
        }else{
            begin=number;
        }
        return begin.replaceAll("(?<=\\d)(?=(?:\\d{3})+$)", ",")+"."+end;
    }

    public static String WGS84toECEF(double latitude, double longitude)
    {
        double X;
        double Y;
        double a = 6378137.0;
        double b = 6356752.31424518;
        double E = (a * a - b * b) / (a * a);
        double COSLAT = Math.cos(latitude * Math.PI / 180);
        double SINLAT = Math.sin(latitude * Math.PI / 180);
        double COSLONG = Math.cos(longitude * Math.PI / 180);
        double SINLONG = Math.sin(longitude * Math.PI / 180);
        double N = a / (Math.sqrt(1 - E * SINLAT * SINLAT));
        double NH = N;
        X = NH * COSLAT * COSLONG;
        Y = NH * COSLAT * SINLONG;
        return X + "@" + Y;
    }


    public static String ECEFtoWGS84(double x, double y, double z)

    {
        double a, b, c, d;
        double Longitude;//经度
        double Latitude;//纬度
        double Altitude;//海拔高度
        double p, q;
        double N;
        a =  6378137.0;
        b =  6356752.31424518;
        c = Math.sqrt(((a * a) - (b * b)) / (a * a));
        d = Math.sqrt(((a * a) - (b * b)) / (b * b));
        p = Math.sqrt((x * x) + (y * y));
        q = Math.atan2((z * a), (p * b));
        Longitude = Math.atan2(y, x);
        Latitude = Math.atan2((z + (d * d) * b * Math.pow(Math.sin(q), 3)), (p - (c * c) * a * Math.pow(Math.cos(q), 3)));
        N = a / Math.sqrt(1 - ((c * c) * Math.pow(Math.sin(Latitude), 2)));
        Altitude = (p / Math.cos(Latitude)) - N;
        Longitude = Longitude * 180.0 / Math.PI;
        Latitude = Latitude * 180.0 / Math.PI;
        return Longitude + "," + Latitude + "," + Altitude;
    }


}